package com.higgins.lawapp.server;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.higgins.lawapp.client.settings.dataItemModel.DataItemContainerDTO;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemDTO;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemInstanceDTO;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemLevelDTO;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemSourceDTO;
import com.higgins.lawapp.client.settings.dataModel.DataSourceDTO;
import com.higgins.lawapp.client.settings.dataModel.DataSourceLevelDTO;
import com.higgins.lawapp.client.settings.rpc.DataSourceService;
import com.higgins.lawapp.server.dataItemModel.DataItem;
import com.higgins.lawapp.server.dataItemModel.DataItemContainer;
import com.higgins.lawapp.server.dataItemModel.DataItemInstance;
import com.higgins.lawapp.server.dataItemModel.DataItemLevel;
import com.higgins.lawapp.server.dataItemModel.DataItemSource;
import com.higgins.lawapp.server.dataModel.DataSource;
import com.higgins.lawapp.server.dataModel.DataSourceLevel;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions.Builder;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;


/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class DataSourceServiceImpl extends RemoteServiceServlet implements DataSourceService {
	DatastoreService myDatastoreService = DatastoreServiceFactory.getDatastoreService();

	@Override
	public List<DataSourceDTO> retrieveDataSources()
			throws IllegalArgumentException {
		PersistenceManager pm=PMF.get().getPersistenceManager();
		List<DataSourceDTO> dataSourceDTOs = new ArrayList<DataSourceDTO>();
		List<DataSource> results; 
		Query query;
		query=pm.newQuery(DataSource.class);
		try	{
			results = (List<DataSource>) query.execute();
			Iterator<DataSource> iterator = results.iterator();
			while(iterator.hasNext())	{
				dataSourceDTOs.add(iterator.next().getDataSourceDTO());
			}
		} catch (Exception e)	{
			System.out.println("DataSourceServiceImpl.retrieveDataSources() Error.." + e.toString());
		}
		pm.close();
		return dataSourceDTOs;
	}

	@Override
	public String insertDataSource(DataSourceDTO dataSourceDTO)
			throws IllegalArgumentException {
		DataSource dataSource = new DataSource(dataSourceDTO);
		String status = "Sucessfully Stored!!!";
		PersistenceManager pm=PMF.get().getPersistenceManager();
		try	{
			pm.makePersistent(dataSource);
		} 
		catch (Exception e)	{
			status = "Unsuccessfully Stored!! " + e.toString();
		}
		finally {
			pm.close();
		}
		return status;
	}


	public String updateDataItemSource(DataItemSourceDTO dataItemSourceDTO)	{
		String status = "Successfully Updated!!!";
		try	{
			PersistenceManager pm=PMF.get().getPersistenceManager();
			Query query=pm.newQuery(DataItemSource.class, "dataSourceName == nameParam");
			query.declareParameters("String nameParam");
			String parameter = dataItemSourceDTO.getDataItemSourceName();
			List<DataItemSource> results = (List<DataItemSource>)query.execute(parameter);
			if (results!=null)	{		// For some reason the dataSource is returned without DataSource Levels (there should be 4)
				DataItemSource dataItemSource;
				if (results.size()>0)	
					dataItemSource = results.get(0);
				else 
					dataItemSource = new DataItemSource();				
				dataItemSource.setDataItemSourceDTO(dataItemSourceDTO);
				pm.makePersistent(dataItemSource);
			}
			pm.close();
		
		} catch (Exception e)	{
			status="Unsuccessfully updated: " + e.toString();
		}
		return status;
		
	}
	
	
	@Override
	public String updateDataSource(DataSourceDTO dataSourceDTO)
			throws IllegalArgumentException {
		String status = "Successfully Updated!!!";
		try	{
			PersistenceManager pm=PMF.get().getPersistenceManager();
			Query query=pm.newQuery(DataSource.class, "dataSourceName == nameParam");
			query.declareParameters("String nameParam");
			String parameter;
			if (dataSourceDTO.getDataSourceNameKey()==null)
				parameter = dataSourceDTO.getDataSourceName();
			else if (dataSourceDTO.getDataSourceNameKey().equals(""))
				parameter = dataSourceDTO.getDataSourceName();
			else
				parameter = dataSourceDTO.getDataSourceNameKey();
			List<DataSource> results = (List<DataSource>)query.execute(parameter);
			if (results!=null)	{		// For some reason the dataSource is returned without DataSource Levels (there should be 4)
				if (results.size()>0)	{
					DataSource dataSource = results.get(0);
					dataSource.update(dataSourceDTO);
					pm.makePersistent(dataSource);
				} else {
					DataSource dataSource = new DataSource(dataSourceDTO);
					pm.makePersistent(dataSource);
				}
			}
			pm.close();
		
		} catch (Exception e)	{
			status="Unsuccessfully updated: " + e.toString();
		}
		return status;
	}		

	
	@Override
	public String deleteDataSource(DataSourceDTO dataSourceDTO)
			throws IllegalArgumentException {
		String status = "Successfully deleted!!!";
		try	{
			PersistenceManager pm=PMF.get().getPersistenceManager();
			if (dataSourceDTO!=null)	{
				Query query=pm.newQuery(DataSource.class);
				query.setFilter("dataSourceName==nameParam");
				query.declareParameters("String nameParam");
				query.deletePersistentAll(dataSourceDTO.getDataSourceName());
			} else	{
				Query query=pm.newQuery(DataSource.class);
				query.deletePersistentAll();
			}
			pm.close();
		} catch (Exception e)	{
			status="Unsuccessfully deleted: " + e.toString();
		}
		return status;
	}		
}
